#include "pageenergy.h"
#include "ui_pageenergy.h"
#include <QDebug>

PageEnergy::PageEnergy(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageEnergy)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pKeyBoard = new SlimNumKeyBoard(this);
    m_pKeyBoard->move(70,380);
    m_pKeyBoard->setFocusPolicy(Qt::NoFocus);
    connect(m_pKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    m_pKeyBoard->hide();

    inputlist.append(ui->lineEdit_auxEgyTotal);
    inputlist.append(ui->lineEdit_regEgyTotal);
    inputlist.append(ui->lineEdit_proEgyTotal);
    for(int i=0; i<inputlist.size(); ++i)
        inputlist.at(i)->installEventFilter(this);

    m_lEnergyList.append(ui->PROEnergyConTotal);
    m_lEnergyList.append(ui->DISEnergyConTotal);
    m_lEnergyList.append(ui->RegEnergyConTotal);
    m_lEnergyList.append(ui->HVACEnergyConTotal);
    //4个元素
}

PageEnergy::~PageEnergy()
{
    delete ui;
}

bool PageEnergy::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<inputlist.size(); ++i)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            if(obj == inputlist.at(i))
            {
                inputlist.at(i)->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
                m_pKeyBoard->show();
                m_pFocusLinedit = inputlist.at(i);
            }
            else
                inputlist.at(i)->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PageEnergy::input_slot(int number)
{
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(m_pFocusLinedit, &numKey);
}

void PageEnergy::backspace_slot()
{
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(m_pFocusLinedit,&backspaceKey);
}

void PageEnergy::cancel_slot()
{
    m_pKeyBoard->hide();
}

void PageEnergy::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageEnergy::setSysEnergy(int sys, uint egy)
{
    m_lEnergyList[sys]->setText(QString("%1").arg(egy));
}

void PageEnergy::setTotalEnergyConsume(uint egy)
{
    ui->TotalEgyCon->setText(QString("%1").arg(egy));
}

void PageEnergy::on_Btn_EnergySet_clicked()
{
    emit egySetValid(true);
    ui->Btn_EnergySet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit egySetValid(false);
        ui->Btn_EnergySet->setEnabled(true);
    });
}

uint PageEnergy::getAuxEgyTotalInput()
{
    if(ui->lineEdit_auxEgyTotal->text().isEmpty())
        return 0;
    return ui->lineEdit_auxEgyTotal->text().toUInt();
}

uint PageEnergy::getProEgyTotalInput()
{
    if(ui->lineEdit_proEgyTotal->text().isEmpty())
        return 0;
    return ui->lineEdit_proEgyTotal->text().toUInt();
}

uint PageEnergy::getRegEgyTotalInput()
{
    if(ui->lineEdit_regEgyTotal->text().isEmpty())
        return 0;
    return ui->lineEdit_regEgyTotal->text().toUInt();
}

void PageEnergy::setPageActive(bool xActive)
{
    ui->Btn_EnergySet->setEnabled(xActive);
}
